#!/bin/bash

Dir_now=`pwd`
if [ $# -ne 3  ]
then
    echo "usage: x60cm.sh fitfile RA DEC"
    exit 0
fi
fitfile=$1
RA=$2
DEC=$3
errorbar=30 #arcsec
Nobj_least=5  # it is the least obj in the fitfile
SolveRadius=0.3  # degree, for solve-field when doing the AStronomy
Ncompstar_c=4  # the number of comparison stars should be more than this number

#IP_server_local=190.168.1.118:8080/gwac
IP_server=190.168.1.25
UploadParameterfile=` echo "http://$IP_server/otFollowUpload.action" | awk '{print($1)}'`

fieldAstrometryCaliFile=$HOME/workspace/fieldAstroCaliFile
stringtimeForMonitorT=`date -u +%Y%m%d`
dir_reduction=$HOME/workspace/reduction/$stringtimeForMonitorT
dir_result=$HOME/workspace/result/$stringtimeForMonitorT
dir_soft60cm=$HOME/software/soft60cm
cd $HOME/workspace/reduction
if test ! -r $stringtimeForMonitorT
then
    echo "making $stringtimeForMonitorT"
    mkdir $stringtimeForMonitorT
fi
cd $HOME/workspace/result
if test ! -r $stringtimeForMonitorT
then
    echo "making $stringtimeForMonitorT in result"
    mkdir $stringtimeForMonitorT
fi
cd $Dir_now
if test ! -r $fitfile
then
    echo "No $fitfile"
    exit 0
fi
cp $fitfile $dir_reduction
cp $dir_soft60cm/* $dir_reduction
cd $dir_reduction

xdefineForCatalog (  )
{
#    echo `pwd`
    dir_getCatalog=$HOME/han/catalogue
    RadiusTemp=0.3   #degree   
    lowMagTemp=-10.0    #brightest object
    largerMagTemp=23.0  #dimest object
}


xdefineForFits (  )
{
#    echo `pwd`

    AstroCaliFile=`echo $fitfile | sed 's/\.fit/.new/g'`
    OUTPUT=`echo $fitfile | sed 's/\.fit/.sex/g'`
    OUTPUT_new=`echo $fitfile | sed 's/\.fit/.sexnew/g'`
    TempUSNORADEC=`echo $fitfile | sed 's/\.fit/_tempRADEC.txt/g'`
    TempUSNOXY=`echo $fitfile | sed 's/\.fit/_tempXY.txt/g'` 
    otxy=`echo $fitfile | sed 's/\.fit/_otxy.txt/g'`
    MonitorFile=`echo $fitfile | sed 's/\.fit/_res.txt/g'`
    matchedfile=`echo $fitfile | sed 's/\.fit/_KnownObjxy.txt/g'`
    compstarsfile=`echo $fitfile | sed 's/\.fit/_compstars.txt/g'`
    OTfile=`echo $fitfile | sed 's/\.fit/_otrealxy.txt/g'`
    deltaMagfile=`echo $fitfile | sed 's/\.fit/_deltaMag.txt/g'`
    OTfinalres=`echo $fitfile | sed 's/\.fit/_otfinalres.txt/g'`
    OTFieldxy=`echo $OTid | awk '{print($1".otxy")}'`
    OTFieldTemp=`echo $OTid | awk '{print($1".tempcomp")}'`
    OTFieldUSNO=`echo $OTid | awk '{print($1".tempusno")}'`
    miniOT_lc=`echo $OTid | awk '{print($1"_miniOT.lc")}'`
    newOT_lc=`echo $OTid | awk '{print($1"_newOT.lc")}'`
    FSC_lc=`echo $OTid | awk '{print($1"_FSC.lc")}'`
    OTWCSfit=`echo $OTid | awk '{print($1"_wcs.new")}'`

}

xastronomy (  )
{
#    echo "To do the xastronomy"
#    echo `pwd`
     echo $fitfile
   # solve-field $fitfile
     #solve-field -scale -H 0.56 -L 0.50 -u app $fitfile
	echo `pwd` 
    rm -rf*.axy *objs-png
    solve-field --use-sextractor -scale -H 0.58 -L 0.53 -u app -3 $RA -4 $DEC -5 $SolveRadius $fitfile
    wait
    if test -r $AstroCaliFile
    then
       echo "AStronomy calibration is successful"
       cp $AstroCaliFile $OTWCSfit
       cp $OTWCSfit $fieldAstrometryCaliFile 
    else
        echo "Astronomy calibration failed, try another image please"
        echo $Dir_now
        exit 0
    fi

}

xGetCatalog (  )
{

    #output includes ra dec b1 b2 R1 R2 I

    ~/usnob1/usnob1 -m 2000000  -r 0.3 -c $RA $DEC -lmr -6,20 -sr > $TempUSNORADEC
    if test -s $TempUSNORADEC
    then
        echo "Geting catalog is successful"
    else
        echo "Geting catalog failed"
        echo $Dir_now
        exit 0
    fi
}

xGetCatalog_temp (  )
{
    echo "xGetCatalog_temp"
    cd $dir_reduction
    if test ! -r tempfile
    then
        mkdir tempfile
    fi
    mv temp*.txt tempfile
    cd $dir_getCatalog
    echo $dir_getCatalog  
    
    echo "python template_extract.py $dir_reduction $RA $DEC $RadiusTemp $lowMagTemp $largerMagTemp"
    python template_extract.py $dir_reduction $RA $DEC $RadiusTemp $lowMagTemp $largerMagTemp
    wait
    if test -s $dir_reduction/temp*.txt
    then
        echo "Catalog is ready"
        
        cd $dir_reduction
        mv temp*.txt $TempUSNORADEC
        #The parameters in $TempUSNORADEC are RA DEC B1 B2 R1 R2
    else
        echo "Catalog is not ready"
        cd $Dir_now
        exit 0
    fi
}


xGetkeywords (  )
{
    echo `pwd`
    echo "xGetkeywords"
    datetime=`gethead $fitfile "date-obs"`
    OTid=`gethead $fitfile "object"`
    FTid=`gethead $fitfile "telescope"`    #follow-up telescope id
    exptime=`gethead $fitfile "exptime"`
    filter=`gethead $fitfile "filter"` #default is R filter right now
    RA=`gethead $fitfile "objctra"`
    DEC=`gethead $fitfile "objctdec"`
    JD=`gethead $fitfile "jd"`
    CCDTYPE=`gethead $fitfile "ccdtype"` #BIAS, DARK, FLAT, LIGHT, FOCUS
    FTid=F01
    echo $datetime $OTid $FTid $exptime $filter $RA $DEC $JD $CCDTYPE
    echo $RA $DEC 
}

xsethead (  )
{
    
    newfitfile=`echo $fitfile | sed 's/\.fits/.fit/g'`
    cp $fitfile  $newfitfile
    echo $newfitfile
    fitfile=`echo $newfitfile`
    echo $fitfile
    OTidname=`echo $fitfile | cut -c1-14`
    sethead -kr X object=$OTidname telescope=WFT  filter=R objctra=$RA objctdec=$DEC jd=50000 CCDTYPE=LIGHT $fitfile

}

xsky2xyOT(  )
{
    #sky2xy $AstroCaliFile $RA $DEC >fotxy
    sky2xy $OTWCSfit $RA $DEC >fotxy
    cat fotxy | grep -v "off" | awk '{if($5>0 && $5<1024 && $6>0 && $6<1024) print($5,$6,$1,$2,"miniOT")}' | column -t >$otxy   #in $otxy, the parameters are x y ra dec,miniOT
    if test -s $otxy
    then
        echo "ot is in the FOV"
        cat $otxy
    else
        echo "OT is not in the FOV"
        echo $Dir_now
        exit 0
    fi
}

xsky2xyTemp(  )
{
    rm tempxy
   # sky2xy $AstroCaliFile @$TempUSNORADEC >tempxy
    sky2xy $OTWCSfit @$TempUSNORADEC >tempxy
   
   paste tempxy $TempUSNORADEC | grep -v "off" | awk '{if($5>0 && $5<1024 && $6>0 && $6<1024)print($5,$6,$7,$8,$9,$10,$11,$12,"99")}' >$TempUSNOXY   #Depend on the parameters after the xGetkeywords. The final parameters are:    x,y, ra, dec, B1, B2,R1,R2,99
    #paste tempxy $TempUSNORADEC | grep -v "off" | awk '{if($5>0 && $6<1024 && $5>0 && $6<1024)print($5,$6,$7,$8,$9,$10,$11,$12,$13)}' | column -t >$TempUSNOXY   #Depend on the parameters after the xGetkeywords. The final parameters are:    x,y, ra, dec, B1, B2,R1,R2, I
    if test -s $TempUSNOXY
    then
        echo "xsky2xy is finished"
        xy2sky $fitfile 512 512 >center.txt
        RA_center=`cat center.txt | awk '{print($1)}'`
        DEC_center=`cat center.txt | awk '{print($2)}'`
        PointError=`skycoor -r $RA  $DEC $RA_center $DEC_center | awk '{print($1/60)}'`  #arcmin
        echo $RA $DEC $RA_center $DEC_center $PointError >>$MonitorFile
    else
        echo "No any source after the xsky2xy"
        echo $Dir_now
        exit 0
    fi
}

xGetstars (  )
{
    echo `pwd`
    echo $fitfile
    ls $fitfile
    echo $OUTPUT

    sex $fitfile  -c  daofind.sex  -DETECT_THRESH 3.0 -ANALYSIS_THRESH 3.0 -CATALOG_NAME $OUTPUT
    Nobj=`wc -l $OUTPUT | awk '{print($1)}'`
    if [ $Nobj > $Nobj_least  ]
    then
        echo "$fitfile has $Nobj stars "
        cp $OUTPUT $OUTPUT_new
    else
        echo "Only $Nobj stars in the $fitfile, please try another image or increase the exptime"
        echo $Dir_now
        exit 0
    fi
}

xcopyResultTobakfile (  )
{
    cp $OTfinalres $miniOT_lc $newOT_lc $FSC_lc $OTFieldTemp  $compstarsfile  $dir_result

}


xgetlc (  )
{
    #cat $OTfinalres
    #echo "before grep"
    #cat $miniOT_lc $newOT_lc $FSC_lc
    cat $OTfinalres | grep "miniOT" >>$miniOT_lc
    cat $OTfinalres | grep "newOT" >>$newOT_lc
    cat $OTfinalres | grep "FSC" >>$FSC_lc
   
    #echo "before sort and uniq"
    #cat $miniOT_lc $newOT_lc $FSC_lc
    
    sort -n -k 19 $miniOT_lc | uniq >lctemp
    mv lctemp $miniOT_lc
    sort -n -k 19 $newOT_lc | uniq >lctemp
    mv lctemp $newOT_lc
    sort -n -k 19 $FSC_lc | uniq >lctemp
    mv lctemp $FSC_lc

    echo "            "
    echo "Light curve for $OTid for the current, first and last image are : "
    cat $OTfinalres
    echo "miniOT"
    head -1 $miniOT_lc 
    tail  -1 $miniOT_lc
    echo "newOT"
    head -1 $newOT_lc 
    tail -1 $newOT_lc
    echo "FSC"
    head -1 $FSC_lc
    tail -1 $FSC_lc
}

xUpdateOTtemp (  )
{

    #cp $TempUSNOXY $OTFieldTemp
    # in $OTFieldxy:   x y ra dec B2 R2 I ottype Obj_label
    cat $OTfinalres | awk '{print($6,$7,$4,$5,$14,$15,$16,$17,$19)}' >$OTFieldxy
    cp $OTFieldxy $fieldAstrometryCaliFile

}

xgetInfor_OT (  )
{
    #to get the light curve, ra dec, picture for identify, comparison objects  of OT,
    #output is $OTfinalres

    if test -s $OTfile
    then
        echo "Detected some objects in this image"
        echo $deltaMagfile
        cat $deltaMagfile

        Ncomp=`wc -l $deltaMagfile | awk '{print($1)}'`
        echo $Ncomp  $filter
        if [ $Ncomp > 0 ]
        then
            if [ "$filter"x == "R"x ]
            then
                echo "$filter band"
                delMag_filter=`cat $deltaMagfile | awk '{print($1-$3)}'`
                #cat $deltaMagfile | awk '{print($1-$3)}' >delMagfileCali 
            elif [ "$filter"x == "B"x ]
            then
                echo "$filter band"
                delMag_filter=`cat $deltaMagfile | awk '{print($1-$2)}'`
                #cat $deltaMagfile | awk '{print($1-$2)}' >delMagfileCali
            elif [ "$filter"x == "I"x ]
            then
                echo "$filter band"
                delMag_filter=`cat $deltaMagfile | awk '{print($1-$4)}'`
                #cat $deltaMagfile | awk '{print($1-$4)}' >delMagfileCali
            else
                echo "$filter band"
                delMag_filter=`cat $deltaMagfile | awk '{print($1-($2+$3)/2)}'`
                #cat $deltaMagfile | awk '{print($1-($2+$3)/2)}' >delMagfileCali 
                #(B+V)/2 for other filter expect for B,R,I in usnob1.0
            fi
        
            Notdetection=`wc -l $OTfile | awk '{print($1)}'`
            for((i=0; i<$Notdetection; i++))
            do
                echo $delMag_filter >>delMagfileCali
            done
            xy2sky -d $OTWCSfit @$OTfile >miniOT_RADEC.cat
            head -1 miniOT_RADEC.cat 
            head -1 $OTfile
            head -1 delMagfileCali
            paste miniOT_RADEC.cat $OTfile delMagfileCali | awk '{if($6>20 && $7>20 && $6<1000 && $7<1000) print(jd,datetime,filter,$1,$2,$6,$7,$8-$19,$9,$10,$11,$12,$13,$14,$15,$16,$17,fitfile,$18)}' jd=$JD  datetime=$datetime filter=$filter  fitfile=$fitfile | sort -n -k  19 | uniq | column -t >$OTfinalres
   #         head -1 $OTfinalres
            #output have 19 parameters, 
            #they are: jd datetime filter ra dec x y Mag_calibrated_To_usno merr ellipticity  calss_stars fwhm  flag  B2 R2 I OTtype fitfilename,Obj_label 
            rm miniOT_RADEC.cat delMagfileCali
            
   		 echo "YES YES YES YES YES YES"
   		 echo "miniOT is for the mini-detected OT"
   		 echo "newOT is for the OT which is not in the errorbars of mini-detected OT. Might be a moving obj or a real astro obj"
   		 echo "FSC is in the errorbars and matches usno,Might be a flare star or a SN"
   		 echo "         "
   		 echo "   RA        DEC      X_temp    Y_temp    Mag_caliedR2 Merr MagUsnoB2 MagUsnoR2  MagUsnoI OTtype  fitsname  Obj_label"
   		 cat $OTfinalres | awk '{print($4,$5,$6,$7,$8,$9,$14,$15,$16,$17,$18,$19)}' | column -t

            
            #========================================
            xgetlc
            xcopyResultTobakfile
            xUpdateOTtemp          
            #========================================
        else
            echo "No comparison objects with S/N >20, this image is bad "
            echo $Dir_now
            exit 0
        fi
    
    else
    
        echo "NO NO NO NO NO NO NO"
        echo "No any new objects are detected"
        echo "NO NO NO NO NO NO NO"
        
        echo "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 NO_OT $fitfile 0" >$OTfinalres
    
    fi
    #===========================
    xUploadResult
    #xReducSameField
    #===========================
}

xcrossmatchFor60cmOT_lc(  ) #for the other images 
{
    echo "==========xcrossmatchFor60cmOT_lc====="
    pwd                                      
    cp $OUTPUT_new newimage.cat              
    if [ $N_compstars -gt $Ncompstar_c   ]    
    then                                     
       echo "compstars num is larger enough"                                                                                                                            
       cat $OTFieldTemp | awk '{print($9,$10,$11,$12,$13,$14,$15,$16,$17)}'>refcomp.cat
    else                                        
       echo "compstars num is too small"    
       cp $OTFieldTemp refcomp.cat          
       echo "=============="                
    fi                                       
    cp $OTFieldUSNO refusno.cat
    cp $OTFieldxy otcoord.cat  #output are x y ra dec B2 R2 I miniOT/FSC/newOT  Obj_label
   # echo "newimage.cat"
   # cat newimage.cat   
   # sleep 5

   # echo "refusno.cat"
   # cat refusno.cat
   # sleep 5

   # echo "otcoord.cat"
   # cat otcoord.cat
   # sleep 5


    echo "To get the OT for $fitfile"
    #input are newimage.cat refusno.cat otcoord.cat,
    #output are match_c.db comp_c.db  nomatch_c.db deltamag_c.db
    ./xcross60cmOT_lc  #need to modified  
   
   # echo "comp_c.db"
   # cat comp_c.db
   # echo "nomatch_c.db"
   # cat nomatch_c.db
   # echo "deltamag_c.db"
   # cat deltamag_c.db
   # sleep 5

    mv comp_c.db  $compstarsfile
    mv nomatch_c.db  $OTfile
    mv deltamag_c.db $deltaMagfile 
    
    #===========================
    xgetInfor_OT
    #===========================
}

xmarkRefStarsToimage (  )
{
    :
}


xcrossmatchFor60cmOT(  )  #for the first image
{
    cp $OUTPUT_new newimage.cat
    cp $TempUSNOXY refusno.cat
    cp $otxy otcoord.cat

    echo "To get the OT for $fitfile"
    #input are newimage.cat refusno.cat otcoord.cat,
    #output are match_c.db comp_c.db  nomatch_c.db deltamag_c.db
    ./xcross60cmOT
    
    mv match_c.db $matchedfile
    mv comp_c.db  $compstarsfile
    mv nomatch_c.db  $OTfile
    mv deltamag_c.db $deltaMagfile 
    
    #cp $TempUSNOXY $OTFieldTemp
    cp $compstarsfile $OTFieldTemp
    cat $OTfile  | awk '{print($1,$2,$3,$4,$14,$15,$16,$17,$18)}' >$OTFieldxy
    echo $OTFieldxy
    #cat $OTFieldxy
    cp $TempUSNOXY $OTFieldUSNO
    cp $OTFieldTemp $OTFieldxy $OTFieldUSNO $fieldAstrometryCaliFile
   
    #===========================
    xmarkRefStarsToimage
    xgetInfor_OT
    #===========================
}

xcrossmatchFor60cmOT_ready (   ) #to get the OT and those variables(brighterning or dimming)
{
    #input:  $OUTPUT $TempUSNOXY  $otxy 
    #$OUTPUT parameters: x y mag merr  ellipticity  class_stars fwhm  flag
    #$TempUSNOXY parameters:  x y ra dec B1 B2  R1 R2 
    #$otxy parameters: x y ra dec (for OT)

    #output is OT, dimT, picture, Variables catalogues
    
#    rm newimage.cat refusno.cat otcoord.cat match_c.db nomatch_c.db comp_c.db deltamag_c.db
    if test -s $OUTPUT_new
    then
        if test -s $TempUSNOXY
        then
            if test -s $otxy
            then
                xcrossmatchFor60cmOT
            else
                echo "No any source in $otxy"
                echo $Dir_now
                exit 0
            fi
        else
            echo "No any source in $TempUSNOXY"
            echo $Dir_now
            exit 0
        fi
    else
        echo "No any source in $OUTPUT"
        echo $Dir_now
        exit 0
    fi
}

xUploadResult (  )
{
    #OTfinalres
    #other parameters, otid?
    #if $17 is NO_OT, it means that no any new object is detected in this image.
    echo `pwd`
    if test -r FirstImage.flag
    then
        echo "===have FirstImage.flag"
        echo "curl $UploadParameterfile   -F tspname=$FTid   -F ot2name=$OTid   -F objlist=@$OTfinalres  -F fitsname=@$fitfile"
        curl $UploadParameterfile   -F tspname=$FTid   -F ot2name=$OTid   -F objlist=@$OTfinalres  -F fitsname=@$fitfile
        rm FirstImage.flag
    else
        echo "===No FirstImage.flag"
        curl $UploadParameterfile   -F tspname=$FTid   -F ot2name=$OTid   -F objlist=@$OTfinalres 

    fi
}

x_xyxymatch (  )
{
    echo "===========x_xyxymatch==========="
    echo $OUTPUT_new $OTFieldTemp
    rm -rf mattmp transform.db $OUTPUT_new

    cd $HOME/iraf
    cp -f login.cl.old login.cl
    echo noao >> login.cl
    echo digiphot >> login.cl
    echo daophot >>login.cl
    echo "cd $dir_reduction" >> login.cl
    echo flpr >> login.cl
    echo "xyxymatch(\"$OUTPUT\",\"$OTFieldTemp\", \"mattmp\",toleranc=40, xcolumn=1,ycolumn=2,xrcolum=1,yrcolum=2,separation=20, matchin=\"triangles\", inter-,verbo-) " >>login.cl
    echo "geomap(\"mattmp\", \"transform.db\", transfo=\"first\", xmin=1, xmax=1024, ymin=1, ymax=1024,fitgeom=\"general\", functio=\"polynomial\",xxorder=2,xyorder=2,xxterms=\"half\",yxorder=2,yyorder=2,yxterms=\"half\",verbos-, inter-)" >>login.cl
    echo "geoxytran(\"$OUTPUT\", \"$OUTPUT_new\",\"transform.db\", transfo=\"first\",geometr=\"geometric\",directi=\"backward\",xcolumn=1,ycolumn=2,calctyp=\"double\",min_sig=7)" >>login.cl
#    echo "geotran(\"$fitfile\",\"$fitfile_geotran\",\"transform.db\",transfo=\"first\")" >>login.cl
    echo logout >> login.cl
    cl < login.cl >xlogfile.log 
    cd $HOME/iraf
    cp -f login.cl.old login.cl
    cd $dir_reduction
    #=============================
    xcrossmatchFor60cmOT_lc
    #=============================

}



xcopyTempAndReduc (  )
{
    cd  $fieldAstrometryCaliFile
    cp $OTFieldTemp $OTFieldxy $OTWCSfit $OTFieldUSNO $dir_reduction
    cd $dir_reduction
    #cp $OTFieldTemp $TempUSNORADEC
    #cp $OTFieldxy $otxy
    N_compstars=`cat $OTFieldTemp | wc -l | awk '{print($1)}'`                                                                                                           
     echo $N_compstars
     if [ $N_compstars -lt $Ncompstar_c   ]  #comparison stars as a reference stars for xyxymatch should not be smaller than 3
     then
        echo "The number of comparison stars is too smaller"
        cp $OTFieldUSNO $OTFieldTemp
      #  cat $OTFieldTemp
     fi

    x_xyxymatch
}

xmkFirstimage (  )
{
    cd $dir_reduction
    echo "xmkFirstimage"
    touch FirstImage.flag
    ls FirstImage.flag
    xastronomy
    xGetCatalog_temp
    #xGetCatalog
    xsky2xyTemp
    xsky2xyOT
    xcrossmatchFor60cmOT_ready
}

xcheckimage (  )
{
    echo "xcheckimage"
    #to check if this field has been astrometrical calibrated or not?
    #if yes, copy the related files and then get the light curve.
    #if not, to make the calibration first by the function xmkFirstimage
    
    cd $fieldAstrometryCaliFile
    if [ -r $OTFieldTemp ] && [ -r $OTFieldxy ]  && [ -r $OTWCSfit   ]
    then
        echo "This field have calibrated fiels"
        xcopyTempAndReduc
    else
        echo `pwd`
        echo "New field, to Astrometry calibration first"
        xmkFirstimage

    fi
}
xsethead
xGetkeywords
xdefineForFits
xdefineForCatalog
xGetstars
xcheckimage
cd $Dir_now
